//
//  LTLXiangCen.m
//  WeiBo
//
//  Created by Apple_Lzzy46 on 16/11/9.
//  Copyright © 2016年 LTL. All rights reserved.
//

#import "LTLXiangCen.h"
#import "LTLPhomeCell.h"

@interface LTLXiangCen ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
//相册
@property (nonatomic, strong) NSMutableArray *PhomeArray;
//约束
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
//UICollectionView
@property (nonatomic, strong) UICollectionView *LTLCollectionView;
//手势
@property (nonatomic, strong) UILongPressGestureRecognizer *longPress;

@end

static NSString *ID =@"LTLPhomeCell";

@implementation LTLXiangCen
///懒加载
- (NSMutableArray *)PhomeArray
{
    if (_PhomeArray ==nil) {
        _PhomeArray = [NSMutableArray array];
    }
    return _PhomeArray;
}
///懒加载
- (UICollectionView *)LTLCollectionView
{
    if (!_LTLCollectionView) {
        ///初始化约束
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        ///间距
        _flowLayout.minimumLineSpacing = 8;
        _flowLayout.minimumInteritemSpacing = 8;
        ///初始化
        _LTLCollectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:_flowLayout];
        ///代理
        _LTLCollectionView.delegate = self;
        _LTLCollectionView.dataSource = self;
        ///背景色
        _LTLCollectionView.backgroundColor = [UIColor colorWithRed:0.8568 green:0.8568 blue:0.8568 alpha:1.0];
        ///注册 cell
        [_LTLCollectionView registerClass:[LTLPhomeCell class] forCellWithReuseIdentifier:ID];
        
        _LTLCollectionView.backgroundColor = [UIColor redColor];
        
        //添加一个长按手势，用来移动cell
        _longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lonePressMoving:)];
        [_LTLCollectionView addGestureRecognizer:_longPress];
        
    }
    return _LTLCollectionView;
}
///手势事件
- (void)lonePressMoving:(UILongPressGestureRecognizer *)longPress
{
    //判断手势状态
    switch (_longPress.state) {
        case UIGestureRecognizerStateBegan: {
            {
                //取出路径并判断该路径是否存在
                NSIndexPath *selectIndexPath = [self.LTLCollectionView indexPathForItemAtPoint:[_longPress locationInView:self.LTLCollectionView]];
                NSLog(@"%ld",selectIndexPath.section);
                if (selectIndexPath == nil) {
                    break;
                }
                // 找到当前的cell
//                LTLCollectionViewCell *cell = (LTLCollectionViewCell *)[self.LTLCollectionView cellForItemAtIndexPath:selectIndexPath];
                // 定义cell的时候btn是隐藏的, 在这里设置为NO
                //                [cell.btnDelete setHidden:NO];
                //移动cell
                [_LTLCollectionView beginInteractiveMovementForItemAtIndexPath:selectIndexPath];
            }
            break;
        }
        case UIGestureRecognizerStateChanged: {
            //移动cell的过程中随时的更新其他cell的状态
            [self.LTLCollectionView updateInteractiveMovementTargetPosition:[longPress locationInView:_longPress.view]];
            break;
        }
        case UIGestureRecognizerStateEnded: {
             //移动cell结束后关闭移动状态
            [self.LTLCollectionView endInteractiveMovement];
            break;
        }
                //移动cell过程中取消
        default: [self.LTLCollectionView cancelInteractiveMovement];
            break;
    }
}
#pragma mark - UICollectionViewDelegateFlowLayout

//定义每个cell Item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = (collectionView.width - 30)/ 3;
    
    return CGSizeMake(width, width);
}
//内边距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}
//返回是否可以移动
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
    ///加号图片不能移动
    if (indexPath.item == self.PhomeArray.count + 1) {
        return NO;
    }
    return YES;
}
//移动处理
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath
{
//    NSIndexPath *selectIndexPath = [self.LTLCollectionView indexPathForItemAtPoint:[_longPress locationInView:self.LTLCollectionView]];
//    // 找到当前的cell
//    LTLCollectionViewCell *cell = (LTLCollectionViewCell *)[self.LTLCollectionView cellForItemAtIndexPath:selectIndexPath];
    //    [cell.btnDelete setHidden:YES];
    ///交换位置
    [self. PhomeArray exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
    [self.LTLCollectionView reloadData];
}
#pragma mark - 代理方法
//行
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"%ld",self.PhomeArray.count);
    //Items保持比PhomeArray数组多一个,有一个是添加按钮
    return self.PhomeArray.count + 1 > 9 ? self.PhomeArray.count : self.PhomeArray.count + 1 ;
}

//cell
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LTLPhomeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID  forIndexPath:indexPath];
    
//    cell.backgroundColor = [UIColor colorWithRed:((10 * indexPath.row * (indexPath.section + 1)) / 255.0) green:((20 * indexPath.row * (indexPath.section + 1))/255.0) blue:((30 * indexPath.row * (indexPath.section + 1))/255.0) alpha:1.0f];
    ///赋值
    ///最后一个是添加按钮
    if (self.PhomeArray.count == indexPath.item) {
        
        cell.phomeImage =[UIImage imageNamed:@"compose_pic_add"];
        
    }
    else    //其他放图片
    {
        cell.phomeImage = self.PhomeArray[indexPath.item];
    
    }
    
    return cell;
}
#pragma mark - 初始化

-(instancetype)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]) != nil) {
        
//        self.LTLCollectionView.frame = frame;
        //添加控件
        [self addSubview:self.LTLCollectionView];
        
    }
    return self;
}
//设置数据
-(void)addPhome:(UIImage*)image
{
    ///显示相册
    self.hidden = NO;
    if (self.PhomeArray.count >= 9) {
        //提示
        [SVProgressHUD showErrorWithStatus:@"最多只能9张照片"];
        return;
    }
    //添加数据
    [self.PhomeArray addObject:image];
    //刷新
    [self.LTLCollectionView reloadData];
}


@end
